package com.yanqu.road.dao.impl.activity.stronghold;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.stronghold.StrongholdWarUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserStrongholdWarDataDaoImpl extends TempDataObjectDao<StrongholdWarUserData> {
    public static String getUserStrongholdBattleDataSql = "select * from t_u_activity_stronghold_user_data where userId=? ";

    public Map<Integer, StrongholdWarUserData> getStrongholdWarUserData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, StrongholdWarUserData> dataMap = new ConcurrentHashMap<>(8);
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserStrongholdBattleDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                StrongholdWarUserData data = parse(rs);
                dataMap.put(data.getActivityId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateUserStrongholdBattleDataSql = "update t_u_activity_stronghold_user_data set power=?,lastRestoreTime=?,wildPatronsId = ?,enterWild = ?,resetPatronsTimeId = ?,guide = ? where userId=? and activityId=?";

    @Override
    protected boolean updateData(StrongholdWarUserData strongholdWarUserData) {
        strongholdWarUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWarUserData.getPower()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWarUserData.getLastRestoreTime()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWarUserData.getWildPatronsId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, strongholdWarUserData.isEnterWild()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWarUserData.getResetPatronsTimeId()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdWarUserData.getGuide()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWarUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWarUserData.getActivityId()));
        if (getDb().executeNoneQuery(updateUserStrongholdBattleDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserStrongholdBattleDataSql = "insert into t_u_activity_stronghold_user_data(userId,activityId,power,lastRestoreTime,wildPatronsId,enterWild,resetPatronsTimeId,guide) values (?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(StrongholdWarUserData strongholdWarUserData) {
        strongholdWarUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWarUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWarUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWarUserData.getPower()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWarUserData.getLastRestoreTime()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWarUserData.getWildPatronsId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, strongholdWarUserData.isEnterWild()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWarUserData.getResetPatronsTimeId()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdWarUserData.getGuide()));
        if (getDb().executeNoneQuery(insertUserStrongholdBattleDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected StrongholdWarUserData parseData(ResultSet rs) throws SQLException {
        StrongholdWarUserData strongholdWarUserData = new StrongholdWarUserData();
        strongholdWarUserData.setUserId(rs.getLong("userId"));
        strongholdWarUserData.setActivityId(rs.getInt("activityId"));
        strongholdWarUserData.setPower(rs.getInt("power"));
        strongholdWarUserData.setLastRestoreTime(rs.getLong("lastRestoreTime"));
        strongholdWarUserData.setEnterWild(rs.getBoolean("enterWild"));
        strongholdWarUserData.setWildPatronsId(rs.getInt("wildPatronsId"));
        strongholdWarUserData.setResetPatronsTimeId(rs.getInt("resetPatronsTimeId"));
        strongholdWarUserData.setGuide(rs.getString("guide"));
        return strongholdWarUserData;
    }
}
